package com.example.shengyin.frontController;

import com.example.shengyin.config.EventConfig;
import com.example.shengyin.entity.Admin;
import com.example.shengyin.entity.Event;
import com.example.shengyin.entity.Page;
import com.example.shengyin.service.EvenService;
import com.example.shengyin.util.Result;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;



@RestController
@RequestMapping("front/event")
@Validated
@CrossOrigin(origins = "*", maxAge = 3600,allowCredentials="true")
//swagger
@Api(tags = "EventFrontController", description = "前端活动")
public class EventFrontController {
    @Autowired
    private EvenService service;

    @Autowired
    private EventConfig eventConfig;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("根据传入的条件获取最新发布，热门点击，最多参与的排序，==》在前端点击按钮时value值传res条件获取最新发布res条件为posttime，" +
            "获取热门点击res条件为visitnums，获取最多参与res条件为joinnums")
    @GetMapping("/search")
    public Result event(@ApiParam(name = "res",value = "活动标题查找") @RequestParam(required = false) String res,
                        @ApiParam(name = "stype",value = "活动类型查找") @RequestParam(required = false) String stype,
                        @ApiParam(name = "addr",value = "地区类型查找") @RequestParam(required = false) String addr,
                        @RequestParam(name = "page",defaultValue = "1") int page,
                        @RequestParam(name = "limit",defaultValue = "10")int limit
    ) {
        List p = service.frontSelect(res, addr,stype,page,limit);
        return p == null ? Result.error("无此专家信息") : new Result(0,p,"查找成功");
    }


    @ApiOperation("活动详情查看")
    @GetMapping(value = "/select/{id}")
    public Result select(@ApiParam(name = "id",value = "活动详情查看") @RequestParam(required = false) int id)
    {
        Event event = service.selectById(id);
        if(event==null)
            return Result.error("没有找到数据");
        event.setVisitnums(event.getVisitnums()+1);
        service.update(event);
        return Result.success(event);
    }

    @GetMapping("stypes")
    @ApiOperation("获取活动类型，前端页面对应兴趣中包含的所有活动类型")
    public Result getStypes() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        ArrayNode jsonArray = JsonNodeFactory.instance.arrayNode();
        for (String value : eventConfig.getStypes()) {
            jsonArray.add(value);
        }
        ObjectNode jsonObject = JsonNodeFactory.instance.objectNode();
        jsonObject.set("stype", jsonArray);
        String json = null;
        try {
            json = objectMapper.writeValueAsString(jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.success(json);

    }

    @GetMapping("addr")
    @ApiOperation("获取活动类型，前端页面对应兴趣中包含的所有活动类型")
    public Result getaddr() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        ArrayNode jsonArray = JsonNodeFactory.instance.arrayNode();
        for (String value : eventConfig.getAddr()) {
            jsonArray.add(value);
        }
        ObjectNode jsonObject = JsonNodeFactory.instance.objectNode();
        jsonObject.set("addr", jsonArray);
        String json = null;
        try {
            json = objectMapper.writeValueAsString(jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.success(json);

    }
}
